﻿LArk.widget.bindClass(
    LArk.widget.BaseWidget.extend({
        options: {
            view: {
                type: "window",
                url: "view.html",
                windowOptions: {
                    width: 440,
                    height: 280
                }
            }
        },
        create: function () {
            
        },
        viewWindow: null,
        selectedView: '',
        drawType: '',
        arrViewVideo: [],
        drawControl: null,
        winCreateOK: function (e, n) {
            this.viewWindow = n;
        },
        activate: function () {
            // this.viewer.scene.globe.depthTestAgainstTerrain = true;
            this.drawControl = new LArk.Draw(this.viewer, {
                hasEdit: false
            })

            //先清除
            this.clearVideo();

            //this.addHistoryVideo();
            this.addHistoryVideo2();
        },
        changePosition: function () {
            var that = this;
            var handler = new Cesium.ScreenSpaceEventHandler(that.viewer.scene.canvas);
            handler.setInputAction(function (movement) {
                var cartesian = LArk.point.getCurrentMousePosition(that.viewer.scene, movement.position);
                if (!cartesian) return;
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        },

        //清楚
        clearVideo:function(){
            if(this.arrViewVideo!=null){
                for (var i = 0; i < this.arrViewVideo.length; i++) {
                    this.arrViewVideo[i].destroy();
                }
            }
            this.arrViewVideo = [];
            this.selectedView = null;
        },
        // 加载已有视频
        addHistoryVideo: function () {
           this.viewer.LArkgis.centerAt({"y":43.819459,"x":125.347527,"z":270.94,"heading":3.7,"pitch":-43,"roll":0})
            // 加载已配置好的视频
            var cartesian = LArk.point.getCenter(this.viewer);
            cartesian = Cesium.Cartesian3.fromDegrees(cartesian.x, cartesian.y, cartesian.z);
            if (!cartesian) return;
            var cameraPosition = Cesium.clone(this.viewer.scene.camera.position);
            var params1 = {
                "type": 3,
                "url": "widgets/videoShed3D/jk.mp4",
                "position":{"x":-2666751.687387643,"y":3759760.3140050094,"z":4393827.489683328},
                "cameraPosition":{"x":-2666770.854148255,"y":3759794.9350685272,"z":4393815.876429808},
                "fov":59.99999999999999,
                "aspectRatio":2.049092849519744,
                "alpha":0.7,"debugFrustum":false,
                "dirObj":{"direction":{"x":0.4639662351806082,"y":-0.8398620297825521,"z":0.28172167744400584},
                "right":{"x":-0.8592106257805773,"y":-0.5040574419245717,"z":-0.08765384068143249},
                "up":{"x":-0.21562104062006943,"y":0.2013898363125332,"z":0.9554840138233237}}
            }
            this.selectedView = new LArk.analysi.VideoShed3D(this.viewer, params1);
            this.arrViewVideo.push(this.selectedView);
        },

        addHistoryVideo2: function () {
            this.viewer.LArkgis.centerAt({"y":31.843255,"x":117.205406,"z":138.72,"heading":179.2,"pitch":-55.7,"roll":0});
            //构造投射体
            this.selectedView = new LArk.analysi.VideoShed3D(this.viewer, {
                "type": 3,
                "url": "../../data/video/ping.mp4",
                "position": { "x": -2479449.488022306, "y": 4823345.718542554, "z": 3345663.0318574635 },
                "cameraPosition": { "x": -2479449.2228132365, "y": 4823356.042524543, "z": 3345690.559405859 },
                "fov": 84.3,
                "aspectRatio": 2.6,
                "alpha": 0.8,
                "debugFrustum": true,
                "dirObj": {
                    "direction": { "x": 0.07331987934745406, "y": -0.31686588753316797, "z": -0.9456321719412325 },
                    "right": { "x": 0.8826585211077188, "y": 0.46201009007723565, "z": -0.08637483304041443 },
                    "up": { "x": -0.4642608430704491, "y": 0.8283373020603265, "z": -0.3135588997412374 }
                }
            });

            this.arrViewVideo.push(this.selectedView);
        },
        // 视频投放
        addVideoShed3D: function (debugFrustum,alpha) { //视频投放
            var that = this;
            var cartesian = LArk.point.getCenter(this.viewer);
            cartesian = Cesium.Cartesian3.fromDegrees(cartesian.x, cartesian.y, cartesian.z);
            if (!cartesian) return;
            var cameraPosition = Cesium.clone(that.viewer.scene.camera.position);
            this.selectedView = new LArk.analysi.VideoShed3D(that.viewer, {
                type: 3,
                url: "widgets/videoShed3D/jk.mp4",
                cameraPosition: cameraPosition,
                position: cartesian,
                alpha: alpha,
                debugFrustum: debugFrustum,
            });
            this.arrViewVideo.push(this.selectedView);
           
        },
        // 图片投放
        addImageShed3D: function (debugFrustum, alpha) {
            var that = this;
            //取屏幕中心点
            var cartesian = LArk.point.getCenter(this.viewer);
            cartesian = Cesium.Cartesian3.fromDegrees(cartesian.x, cartesian.y, cartesian.z);
            if (!cartesian) return;
            var cameraPosition = Cesium.clone(that.viewer.scene.camera.position);
            //构造投射体
            this.selectedView = new LArk.analysi.VideoShed3D(that.viewer, {
                type: 2,
                url: "widgets/videoShed3D/gugong.jpg",
                cameraPosition: cameraPosition,
                position: cartesian,
                alpha: alpha,
                debugFrustum: debugFrustum,
            });
            this.arrViewVideo.push(this.selectedView);
        },
        // 文本投放
        addTextShed3D: function (debugFrustum,alpha) {
            var cartesian = LArk.point.getCenter(this.viewer);
            cartesian = Cesium.Cartesian3.fromDegrees(cartesian.x, cartesian.y, cartesian.z);
            if (!cartesian) return;
            var cameraPosition = Cesium.clone(this.viewer.scene.camera.position);
            //构造投射体
            this.selectedView = new LArk.analysi.VideoShed3D(this.viewer, {
                type: 4,
                text: "视频分析",
                cameraPosition: cameraPosition,
                position: cartesian,
                alpha: alpha,
                debugFrustum: debugFrustum,
                textStyles: {
                    font: '50px 楷体',
                    fill: true,
                    fillColor: new Cesium.Color(1.0, 1.0, 0.0, 1.0),
                    stroke: true,
                    strokeWidth: 2,
                    strokeColor: new Cesium.Color(1.0, 1.0, 1.0, 0.8),
                    backgroundColor: new Cesium.Color(1.0, 1.0, 1.0, 0.1),
                    textBaseline: 'top',
                    padding: 40,
                }
            });
            this.arrViewVideo.push(this.selectedView);
        },
        // 颜色投放
        addColorShed3D: function(debugFrustum,alpha) {
            var cartesian = LArk.point.getCenter(this.viewer);
            cartesian = Cesium.Cartesian3.fromDegrees(cartesian.x, cartesian.y, cartesian.z);
            if (!cartesian) return;
            var cameraPosition = Cesium.clone(this.viewer.scene.camera.position);
            //构造投射体
            this.selectedView = new LArk.analysi.VideoShed3D(this.viewer, {
                type: 1,
                color: Cesium.Color.CYAN.withAlpha(0.5),
                cameraPosition: cameraPosition,
                position: cartesian,
                alpha: alpha,
                debugFrustum: debugFrustum
            });
            this.arrViewVideo.push(this.selectedView);
        },
        disable: function () {
            this.clearVideo();
            this.viewWindow = null;
            this.viewer.scene.globe.depthTestAgainstTerrain = false;
        },
        cameraState: function (cameraFollow) {
            var that =this;
            if (cameraFollow) {
                this.viewer.camera.direction = that.selectedView.camera.direction;
                this.viewer.camera.right = that.selectedView.camera.right;
                this.viewer.camera.up = that.selectedView.camera.up;
            }
        },
        seletCamera() {
            var that =this;
            this.drawControl.on(LArk.draw.event.DrawMouseMove, function (e) {
                var pos = e.position;
                if (that.drawType === "ViewPos")
                that.arrViewVideo[that.arrViewVideo.length - 1].position = pos;
                if (that.drawType === "CameraPos")
                that.arrViewVideo[that.arrViewVideo.length - 1].cameraPosition = pos;
            });
            this.drawControl.on(LArk.draw.event.DrawCreated, function (e) {
                that.drawControl.clearDraw();
            });
            if (this.selectedView == null) return;
                this.drawType = "CameraPos";
                this.drawControl.clearDraw();
                this.drawControl.startDraw({ type: "point" });
            }
        
    })
);